"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : ALTER PROCEDURE
Case Name   : 修改运行时参数enable_default_ustore_table设置参数值为当前会话中的
              值
Create At   : 2022/05/11
Owner       : @zou_jialiang050
Description :
    1.创建存储过程
    2.修改运行时参数enable_default_ustore_table为当前会话中的值
    3.新会话调用存储过程
    4.查询存储过程中建的表属性
    5.清理环境
Expect      :
    1.成功
    2.成功
    3.成功
    4.返回调用存储过程所建表属性是否含USTORE与当前会话参数值有关
    5.成功
History     :
"""

import os
import unittest
from testcase.utils.Constant import Constant
from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class PROCEDURE(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f'-----{os.path.basename(__file__)} start-----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.Con = Constant()
        self.common = Common()
        self.procedure = 'p_alter_procedure_0051'
        self.table = 't_alter_procedure_0051'

    def test_alter_procedure(self):
        step = 'step1:创建存储过程 expect:成功'
        self.logger.info(step)
        create_user = self.primary_sh.execut_db_sql(f'''
            create or replace procedure {self.procedure}()
            is
            begin
            drop table if exists {self.table};
            create table {self.table}(c_int int);
            end;
            ''')
        self.logger.info(create_user)
        self.assertIn(self.Con.CREATE_PROCEDURE_SUCCESS_MSG, create_user,
                      '执行失败' + step)

        step = 'step2:修改运行时参数enable_default_ustore_table为当前会话中的' \
               '值 expect:成功'
        self.logger.info(step)
        cmd = f'''show enable_default_ustore_table;alter procedure 
            {self.procedure}() set enable_default_ustore_table from current;'''
        alter_procedure = self.primary_sh.execut_db_sql(cmd)
        self.logger.info(alter_procedure)
        self.assertIn(self.Con.ALTER_FUNCTION_SUCCESS_MSG, alter_procedure,
                      '执行失败' + step)

        step = 'step3:新会话调用存储过程 expect:成功'
        self.logger.info(step)
        call_pro = self.primary_sh.execut_db_sql(
            f'''call {self.procedure}();''')
        self.logger.info(call_pro)
        self.assertNotIn('ERROR', call_pro, '执行失败' + step)
        self.assertIn(self.procedure, call_pro, '执行失败' + step)

        step = 'step4:查询存储过程中建的表属性 ' \
               'expect:返回调用存储过程所建表属性是否含USTORE与当前会话参数值' \
               '有关'
        self.logger.info(step)
        select_properties = self.primary_sh.execut_db_sql(
            f'''select reloptions from pg_class where relname='{self.table}';
            ''')
        self.logger.info(select_properties)
        flg = str(alter_procedure.splitlines()[-4]).strip()
        if flg == 'on':
            self.assertIn('storage_type=USTORE', select_properties,
                          '执行失败' + step)
        else:
            self.assertNotIn('storage_type=USTORE', select_properties,
                             '执行失败' + step)

    def tearDown(self):
        step = 'step5:清理环境 expect:清理环境成功'
        self.logger.info(step)
        clean_environment = self.primary_sh.execut_db_sql(
            f'''drop table {self.table};
            drop procedure {self.procedure};''')
        self.logger.info(clean_environment)
        self.assertIn(self.Con.TABLE_DROP_SUCCESS, clean_environment,
                      '执行失败' + step)
        self.assertIn(self.Con.DROP_PROCEDURE_SUCCESS_MSG, clean_environment,
                      '执行失败' + step)
        self.logger.info(f'-----{os.path.basename(__file__)} end-----')
